package gui.skolnikwin;

import gui.nastaveni.CeskyJazykNastaveniPanel;
import gui.nastaveni.MatematikaNastaveniPanel;
import gui.nastaveni.NastaveniDialog;
import gui.ulohy.CeskyJazyk;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import utils.Nastroje;

import model.NastaveniUlohy;
import model.Zadani;

/**
 * Trida hlavniho okna aplikace.
 * @author schovjan
 * @date Jan 9, 2012
 */
public class SkolnikWin extends JFrame {

    private static final long serialVersionUID = 1L;
    private SkolnikWinManager winManager = null;
    
    private Dimension velikost = new Dimension(380, 500);
    private JTabbedPane tpnUlohy = null;
    private NastaveniDialog nastaveniDialog = null;
    private NastaveniUlohy nastaveni = null;
    

    /**
     * Konstruktor
     */
    public SkolnikWin() {
	super();
	Nastroje.inicializujSlozkuAplikace();
	this.winManager = new SkolnikWinManager(this);
	this.nastaveni = new NastaveniUlohy();
	createGUI();
    }


    /**
     * Vytvoreni GUI
     */
    private void createGUI() {
		// hlavni nadpis
	JLabel lNadpis = new JLabel("Procvičujeme si...");
	lNadpis.setFont(Nastroje.fVelky);
	lNadpis.setSize(new Dimension((int) velikost.getWidth(), 50));
		// ouska s ulohami
	CeskyJazykNastaveniPanel cestinaNastaveni = new CeskyJazykNastaveniPanel(winManager);
//	MatematikaNastaveniPanel matematikaNastaveni = new MatematikaNastaveniPanel(winManager);
	tpnUlohy = new JTabbedPane();
	tpnUlohy.add("český jazyk", cestinaNastaveni);
	//tpnUlohy.add("matematika", matematikaNastaveni);
	
		// dialog s nastavenim
	nastaveniDialog = new NastaveniDialog(nastaveni);
	
		// zacit ulohu procvicovani dle aktualniho tabu
	JButton btnStart = new JButton("Začít");
	btnStart.addActionListener(new AbstractAction() {

	    private static final long serialVersionUID = 1L;


	    @Override
	    public void actionPerformed(ActionEvent e) {
		zacitUlohu();
	    }
	});
	btnStart.setFont(Nastroje.fStredni);
	btnStart.setBackground(Nastroje.cZelena);
	btnStart.setMinimumSize(new Dimension(150, 30));
		// konec aplikace
	JButton btnKonec = new JButton("Konec");
	btnKonec.addActionListener(new AbstractAction() {

	    private static final long serialVersionUID = 1L;


	    @Override
	    public void actionPerformed(ActionEvent e) {
		System.exit(0);
	    }
	});
	btnKonec.setFont(Nastroje.fStredni);
	btnKonec.setBackground(Nastroje.cCervena);
	btnKonec.setMinimumSize(new Dimension(90, 30));
		// nastaveni aplikace
	JButton btnNastaveni = new JButton("Nastavení");
	btnNastaveni.addActionListener(new AbstractAction() {

	    private static final long serialVersionUID = 1L;


	    @Override
	    public void actionPerformed(ActionEvent e) {
		nastaveniPanelShow();
	    }
	});
	btnNastaveni.setFont(Nastroje.fStredni);
	btnNastaveni.setMinimumSize(new Dimension(150, 30));
	
	JPanel panel = new JPanel();
	GroupLayout layout = new GroupLayout(panel);
	layout.setAutoCreateGaps(true);
	layout.setAutoCreateContainerGaps(true);
	panel.setBackground(Nastroje.cZluta);
	panel.setLayout(layout);
	layout.setHorizontalGroup(layout.createSequentialGroup()
		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
			.addComponent(lNadpis, Alignment.CENTER)
			.addComponent(tpnUlohy, Alignment.CENTER)
			.addComponent(btnStart, Alignment.CENTER)
			.addComponent(btnNastaveni, Alignment.CENTER)
			.addComponent(btnKonec, Alignment.CENTER)));
	layout.setVerticalGroup(layout.createSequentialGroup()
		.addGroup(layout.createSequentialGroup()
			.addComponent(lNadpis))
		.addGroup(layout.createSequentialGroup()
			.addComponent(tpnUlohy)
			.addComponent(btnStart)
			.addComponent(btnNastaveni)
			.addComponent(btnKonec)));
	
	this.add(panel);
	this.setLocationRelativeTo(getContentPane());
	this.getContentPane();
	this.setSize(velikost);
	this.setTitle("Procvičujeme si...");
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	int w = this.getSize().width;
	int h = this.getSize().height;
	int x = (dim.width-w)/2;
	int y = (dim.height-h)/2;
	this.setLocation(x, y);
    }


    /**
     * Zobrazi info dialog
     * @param textMessage
     */
    public void showInfoMessage(String textMessage) {
	JOptionPane.showMessageDialog(this, "\n" + textMessage + "\n\n", "Informace", JOptionPane.INFORMATION_MESSAGE);
    }


    /**
     * Zobrazi varovny dialog
     * @param textMessage
     */
    public void showAlertMessage(String textMessage) {
	JOptionPane.showMessageDialog(this, "\n" + textMessage + "\n\n", "Upozornění", JOptionPane.WARNING_MESSAGE);
    }


    /**
     * Zobrazi chybovy dialog
     * @param textMessage
     */
    public void showErrorMessage(String textMessage) {
	JOptionPane.showMessageDialog(this, "\n" + textMessage + "\n\n", "Chyba", JOptionPane.ERROR_MESSAGE);
    }


    private void zacitUlohu() {
	if (tpnUlohy.getSelectedComponent() instanceof CeskyJazykNastaveniPanel) {
	    nastaveni.setZadani(((CeskyJazykNastaveniPanel) (this.tpnUlohy.getComponentAt(0))).getZadaniUloh());
	    if (nastaveni.getZadani().isEmpty()) {
		showErrorMessage("Nejprve vyber co chceš procvičovat.");
	    } else {
		showCestina();
	    }
	}
	if (tpnUlohy.getSelectedComponent() instanceof MatematikaNastaveniPanel) {
	    System.out.println("matematika");
	}
    }


    public void showCestina() {
	try {
	    List<Zadani> priklady = Nastroje.getZadani(nastaveni);
	    new CeskyJazyk(winManager, nastaveni, priklady).setVisible(true);
	    nastaveni.setZadani(null);
	} catch (Exception e) {
	    showErrorMessage(e.toString());
	    e.printStackTrace();
	}
    }


    public void showMatematika(NastaveniUlohy nastaveni) {
    }
    
    private void nastaveniPanelShow() {
	nastaveniDialog.setVisible(true);
    }
    
}
